package cn.psvmc.cxzapi.service.customer;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.mapper.customer.TCustomerCardMapper;
import cn.psvmc.cxzapi.mapper.customer.TCustomerMapper;
import cn.psvmc.cxzapi.model.customer.TCustomer;
import cn.psvmc.cxzapi.model.customer.TCustomerCard;
import cn.psvmc.cxzapi.service.alipay.AlipayCreate;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户会员卡表 服务实现类
 *
 * @Author auto
 * @Date 2025-01-04
 */
@Service
public class TCustomerCardService extends AbstractService<TCustomerCardMapper, TCustomerCard> {

    @Resource
    private TCustomerCardMapper tCustomerCardMapper;
    @Resource
    private TCustomerMapper tCustomerMapper;

    /**
     * @Description 保存会员卡信息
     * @Date 2025/1/4 19:08
     * @Author YWX
     * @Param [card]
     * @Return int
     **/
    public void add(TCustomerCard card) {
        tCustomerCardMapper.save(card);
        if (card.getSuccess().equals("true")) {
            tCustomerMapper.updateBizCardNoByOpenid(card.getBizCardNo(), card.getOpenId());
        }
    }

    /**
     * @Description 客户会员卡列表
     * @Date 2025/1/4 19:13
     * @Author YWX
     * @Param [userid]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listCard(int userid) {
        if (!ToolUtils.isTrueInteger(userid)) {
            return new ArrayList<>();
        }
        String openId = tCustomerMapper.getOpenIdByCustomerId(userid);
        if (ToolUtils.isEmpty(openId)) {
            return new ArrayList<>();
        }
        return tCustomerCardMapper.listCard(openId);
    }

    /**
     * @Description 会员卡查询
     * @Date 2025/1/4 22:09
     * @Author YWX
     * @Param [userid, bizCardNo]
     * @Return java.lang.Object
     **/
    public Object detail(int userid, String bizCardNo) {
        if (!ToolUtils.isTrueInteger(userid)) {
            return null;
        }
        String openId = tCustomerMapper.getOpenIdByCustomerId(userid);
        if (ToolUtils.isEmpty(openId)) {
            return null;
        }
        return AlipayCreate.cardQuery(bizCardNo, openId);
    }

    /**
     * @Description 是否是会员
     * @Date 2025/1/4 22:20
     * @Author YWX
     * @Param [customerid]
     * @Return boolean
     **/
    public int isMember(int customerid) {
        if (!ToolUtils.isTrueInteger(customerid)) {
            return 0;
        }
        String bizCardNo = tCustomerMapper.getBizCardNoByCustomerId(customerid);
        if (ToolUtils.isEmpty(bizCardNo)) {
            return 0;
        } else {
            return 1;
        }
    }

    /**
     * @Description 更新客户会员卡基础信息
     * @Date 2025/1/5 17:51
     * @Author YWX
     * @Param [customer]
     * @Return void
     **/
    public void updateCardInfoByOpenId(TCustomer customer) {
        tCustomerMapper.updateCardInfoByOpenId(customer);
    }
}
